﻿using UnityEngine;
using System.Collections;

public class UIMaterial : MonoBehaviour 
{
	public int matIndex = 0;
	
	[HideInInspector][SerializeField] Material mMaterial;
	public Material material
	{
		get
		{
			if(mMaterial == null)
			{
				MeshRenderer render = gameObject.GetComponent<MeshRenderer>();
				if(render != null)
				{
					if(render.materials.Length > matIndex)
						mMaterial = render.materials[matIndex];
				}
			}
			
			return mMaterial;
		}
		set
		{
			if(mMaterial != value)
			{
				mMaterial = value;
				
				if(!isUnload && !packaging)
				{
					if(value != null)
					{						
//						mustLoad = true;
						srcPath = UIManager.GetMaterialAssetPath(value);
					}
					else
					{
//						mustLoad = false;
						srcPath = "";
					}
					
					mustLoad = false;
				}
			}
		}
	}
	
	public string srcPath = "";
	public bool mustLoad = true;
	public bool isUnload = false;
	public bool packaging = false;
	
#if UNITY_EDITOR
	//更新资源路径记录
	public void UpdateSrcPath(bool onEnable)
	{
		if(Application.isEditor && !Application.isPlaying && !packaging)
		{
			if(mMaterial != null)
			{
				srcPath = UnityEditor.AssetDatabase.GetAssetPath(mMaterial);
			}
			else if(!onEnable)
				srcPath = "";						
		}
	}
#endif
}
